/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package crawler;

import com.db.jpa.LabeltypesJpaController;
import db.Label;
import db.Labeltypes;
import java.io.File;
import java.util.Collection;
import java.util.List;
import phoenixx.Utility;

/**
 *
 * @author devashish
 */
public class Crawl implements Runnable {

    static long baseId = 0;
    File directory;

    public Crawl(long baseId, File directory) {
        this.baseId = baseId;
        this.directory = directory;
    }
    
    void scanFolders(long parentLabelId, File directory) {        
        baseId++;
        /*MAKE ENTRY INTO DATABASE FOR folder and parentLabelId*/
        System.out.println("folder:" + baseId + ":" + parentLabelId + ":" + directory.getAbsolutePath());
        parentLabelId = baseId;
        try {
            File[] list = directory.listFiles();
            for (int i = 0; i < list.length; i++) {                
                if (list[i].isFile()) {
                    /*MAKE ENTRY INTO LABELTYPES TABLE IF extension IS NOT THERE*/
                    /*FETCH LABELID FOR THIS LABLETYPE*/                    
                    String labelType = getLabelType(list[i].getName());
                    //labelTypeHandling(labelType);
                    baseId++;
                    /*MAKE ENTRY INTO DATABASE FOR file and baseId*/
                    System.out.println("file:" + baseId + ":" + parentLabelId + ":" + list[i].getAbsolutePath());
                } else if (list[i].isDirectory()) {                    
                    scanFolders(parentLabelId, new File(list[i].getAbsolutePath()));
                }                
            }
        } catch (NullPointerException er) {
            System.out.println("ERROR: " + er);
        }       
    }

    public void run() {
        scanFolders(baseId, directory);
    }

    /*FOR TESTING PURPOSE ONLY*/
    public static void main(String[] args) {
        long baseId = -1; /*FETCH THIS VALUE FROM THE DATABASE*/
        //baseId = baseId + 1; //incrementing for the input of next labelId
        File directory = new File("D:/devashish/com");
        Crawl crawl = new Crawl(baseId, directory);
        Thread t = new Thread(crawl);
        t.start();
    }

    private String getLabelType(String name) {
        String ext = Utility.getExtension(name);
        return ext;
    }

    private void labelTypeHandling(String name) {
      if(name != null) {
         /* Labeltypes labelTypes = new Labeltypes();
          Collection<Label> labelCollection = labelTypes.getLabelCollection();
         */
        /*The following code is for making an entry into the database, first need to check whether an entry
          already exist for the same or not*/
          /*Labeltypes labeltypes = new Labeltypes();
          labeltypes.setLabelType(name);
          LabeltypesJpaController labelTypesJpaController = new LabeltypesJpaController();
          labelTypesJpaController.create(labeltypes);*/

      }
    }

}